package org.springframework.sandbox.nettymvcpost.nettymvc;

import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;

import java.net.URI;

public class StringToFullHttpRequestDecoder extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        String taskid="";
        ///Netty/Spring
        //URI uri = new URI("/Netty/" + taskid + "/Spring");
        RequestFuture requestFuture=JSONObject.parseObject(s, RequestFuture.class);

        //URI uri=new URI("/getUserNameById");
        String[] patharray=requestFuture.getPath().split("\\.");
        String path= patharray[patharray.length-1];
        URI uri=new URI("/"+path);
        FullHttpRequest fullHttpRequest=new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,uri.toASCIIString());

        ByteBuf byteBuf= fullHttpRequest.content();

        //channelHandlerContext.write(fullHttpRequest);
        channelHandlerContext.fireChannelRead(fullHttpRequest);
    }
}
